package cn.itcast.servlet;

import cn.itcast.domain.City;
import cn.itcast.domain.User;
import cn.itcast.service.CityService;
import cn.itcast.service.UserService;
import cn.itcast.service.impl.CityServiceImpl;
import cn.itcast.service.impl.UserServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;


@WebServlet("/user")
public class UserServlet extends HttpServlet {
    private UserService service = new UserServiceImpl();
    private CityService cityService = new CityServiceImpl();

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("findAll".equals(method)) {
            findAll(req, resp);
        }
        if ("add".equals((method))) {
            add(req, resp);
        }
        if ("delete".equals(method)) {
            delete(req, resp);
        }
        if ("findById".equals(method)) {
            findById(req, resp);
        }
        if ("update".equals(method)) {
            update(req, resp);
        }

    }



    // TODO: 2020年7月14日18:11:40  修改用户信息
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取表单所有数据
        Map<String, String[]> map = req.getParameterMap();
        //利用beanUtils将数据封装到对象中
        User user = new User();
        try {
            BeanUtils.populate(user, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //调用service,修改用户
        service.update(user);
        //重新查询数据库,遍历
        req.getRequestDispatcher("/user?method=findAll").forward(req, resp);

    }

    // TODO: 2020年7月14日16:53:00  根据用户id查询用户信息
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户id信息
        String uid = req.getParameter("uid");
        //System.out.println(uid);
        //调用service,查询一条用户信息
        User user = service.findById(uid);

        List<City> cityList = cityService.findAll();
        //将对象存储到域中
        req.setAttribute("user", user);
        req.setAttribute("citys", cityList);
        //跳转到,update.jsp页面
        req.getRequestDispatcher("/update.jsp").forward(req, resp);

    }
    // TODO: 2020年7月14日13:46:37   根据用户id删除一条用户的功能

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取用户id
        String uids = req.getParameter("uid");
        //System.out.printf(uid);
        //调用service,根据id删除
        service.deleteByIds(uids);

        //重新访问数据库,遍历
        req.getRequestDispatcher("/user?method=findAll").forward(req, resp);


    }

    //TODO: 2020年7月13日16:31:20  查询所有功能
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取当前页是第几页
        String pageN = req.getParameter("pageNum");
        //如果pageN不为null 并且不为空,转为int类型
        Integer pageNum =0;
        if (pageN != null && !"".equals(pageN)){
             pageNum = Integer.parseInt(pageN);
        }
        //
        if (pageNum == null){
            pageNum = 1;
        }

        //调用service方法查询所有用户
        List<User> users = service.findAll(pageNum);

        PageInfo<User> pageInfo = new PageInfo<>(users);
        System.out.println(pageInfo.getList());
        //System.out.println(users);
        //将获取的所有用户存到域中
        req.setAttribute("pageInfo", pageInfo);
        //跳转到list.jsp页面进行遍历
        req.getRequestDispatcher("list.jsp").forward(req, resp);

    }

    // TODO: 2020/7/14  添加用户功能
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取所有参数封装到map中
        Map<String, String[]> map = req.getParameterMap();
        User user = new User();
        try {
            //将页面中的数据封装到对象中
            BeanUtils.populate(user, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //System.out.println(user);
        //调用service,添加用户
        service.add(user);
        //刷新数据库,重新遍历
        req.getRequestDispatcher("/user?method=findAll").forward(req, resp);
    }


}
